package com.yl.flink.processor;

import com.yl.constant.Const;
import com.yl.entity.PrimaryWarn;
import com.yl.util.SUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple3;

/**
 * @author wlf
 * @since 2022/8/18
 */
public class WarnStatusMapFunc implements MapFunction<PrimaryWarn, Tuple3<String, Integer, Integer>> {

    private final String projectName;

    public WarnStatusMapFunc(String projectName) {
        this.projectName = projectName;
    }

    /**
     * 测点状态(-1离线，0在线，1紧急，2严重，3一般)
     * 这里把告警的状态缓存到redis，即key为测点id:指标code，value为1|2|3
     */
    @Override
    public Tuple3<String, Integer, Integer> map(PrimaryWarn primaryWarn) throws Exception {
        // 后缀为 测点id和指标code
        Integer targetId = primaryWarn.getMultiDataEntity().getTarget().getId();
        Integer quotaCode = primaryWarn.getQuota().getCode();
        String key = SUtil.fmtRedisKey(projectName, Const.REDIS_KEY_STATUS_WARN, targetId, quotaCode);
        // 第三个-1代表没有失效时间
        return Tuple3.of(key, primaryWarn.getWarn().getLevel(), -1);
    }

}
